<template>
  <layout>
    <template slot="title">
      {{ (currentHouseHolder.member_nam ? currentHouseHolder.member_nam + '的' : '') + '家庭成员列表'  }}
      <a-button
        class="action-button"
        type="link"
        :disabled="!currentHouseHolder.member_nam"
        @click="handlerTableAdd"
      >[添加成员]</a-button>
      <a-button
        type="link"
        size="small"
        @click="$refs.FamilyImport.handlerUploadExecl()"
      >
        [导入原单机版家庭成员数据]
      </a-button>
      <a-button
        type="link"
        size="small"
      >
        <a :href="$downloadMembersUrl" target="_blank">[数据模板下载]</a>
      </a-button>
    </template>
    <!-- 顶部操作 -->
    <!-- <section
      slot="search"
    >
      <custom-form
        ref="search-form"
        class="search-form"
        v-model="tableSearch"
        layout="inline"
        :FormList="searchColumns"
      > -->
        <!-- <a-button
          class="action-button"
          type="primary"
          @click="search()"
        >搜索</a-button> -->
        <!-- {{ currentHouseHolder.member_nam }}的家庭成员列表 -->
      <!-- </custom-form> -->
      <!-- <a-input placeholder="产品名称或者编号" v-model="tableSearch.searchKey" allowClear style="width: 200px;" /> -->
    <!-- </section> -->

    <!-- 产品列表 -->
    <a-table
      :dataSource="tableData"
      :columns="tableColumns"
      :rowKey="record => record.usr_id"
      :pagination="false"
      :loading="tableLoading"
      :customRow="customTableRow"
      :scroll="{
        x: true
      }"
      bordered
    >
      <template
        slot="role"
        slot-scope="row"
      >
        {{getLabel(row.role, [], 'role')}}
      </template>
      <template
        slot="member_sex"
        slot-scope="row"
      >
        {{getLabel(row.member_sex, [], 'member_sex')}}
      </template>
      <template
        slot="member_bank_code"
        slot-scope="row"
      >
        {{getLabel(row.member_bank_code, [], 'member_bank_code')}}
      </template>
      <!-- 按钮区 -->
      <template
        slot="action"
        slot-scope="row"
      >
        <a-button
          class="action-button"
          type="link"
          size="small"
          @click="handleTableEdit(row)"
        >编辑</a-button>
        <a-popconfirm
          title="您确认要删除该条数据?"
          ok-text="确认"
          cancel-text="取消"
          @confirm="tableDel(row)"
        >
          <a-button
            class="action-button"
            type="link"
            size="small"
          >删除</a-button>
        </a-popconfirm>
        <a-button
          class="action-button"
          type="link"
          size="small"
          :disabled="+row.role === 1"
          @click="handlerSetHolder(row)"
        >设置为户主</a-button>
        <!-- auth="member.set-householder" -->

        <!-- <a-button
          class="action-button"
          type="primary"
          size="small"
          @click="handleTableEdit(row)"
        >授权</a-button>
        <a-button
          class="action-button"
          type="primary"
          size="small"
          @click="handleTableEdit(row)"
        >项目管理</a-button> -->
      </template>
    </a-table>

    <!-- 产品列表分页器 -->
    <!-- <a-pagination
      slot="paging"
      :pageSizeOptions="pageSizes"
      :total="total"
      showSizeChanger
      :pageSize="pageSize"
      v-model="currentPage"
      @showSizeChange="changePageSize"
      @change="changePage"
    >
    </a-pagination> -->

    <!-- 编辑产品数据 -->
    <a-modal
      :visible="visible"
      :title="visibleTitle"
      centered
      @cancel="visible = false"
      @ok="handleSubmit"
    >
      <custom-form
        ref="custom-form"
        class="custom-form"
        v-model="currentRow"
        :FormList="formColumns"
        :mode="formMode"
        v-bind="formItemLayout"
        layout="horizontal"
      ></custom-form>
      <!-- <section slot="footer"> -->
      <!-- <a-button
        type="primary"
        @click="handleSubmit"
      >提交</a-button> -->
      <!-- </section> -->
    </a-modal>

    <FamilyImport ref="FamilyImport" />

  </layout>
</template>

<script lang="ts">
import { Mixins, Component, Watch, Vue, Prop } from 'vue-property-decorator'
import { State, Mutation, Getter, namespace } from 'vuex-class'
import EventsMixin from '@/mixin/events'
import { TableMixin, AuthMixin } from '@/mixin'

import { TableLayout } from '@/layouts'
import CustomForm from '@/components/src/Form'
import { userServices, IAdmin } from '@/common/services'
import { famerServices, IMember } from '@/common/services/famer'
import { commonServices } from '@/common/services/common'
import { memberRole } from '@/common/enum'
import FamilyImport from './FamilyImport.vue'

@Component({
  components: {
    Layout: TableLayout,
    CustomForm,
    FamilyImport
  }
})
export default class AdminList extends Mixins(
  EventsMixin,
  TableMixin,
  AuthMixin
) {
  @Prop({
    type: String
  })
  public currentAreaId!: string
  @Prop({
    type: Object
  })
  public currentMember!: IMember

  public currentHouseHolder: Partial<IMember> = {}

  public commonParams = {
    s_a_id: '',
    family_id: ''
  }

  // 产品列表搜索条件
  public tableSearch = {
    role: '',
    switch: '2'
  }

  // 产品列表表格信息
  public columns: ITable.IColumn[] = [
    {
      title: '与户主关系',
      key: 'role',
      scopedSlots: { customRender: 'role' },
      width: 120,
      form: {
        type: 'a-select',
        placeholder: '选择与户主关系',
        dict: memberRole
      }
    },

    {
      title: '姓名',
      dataIndex: 'member_nam',
      key: 'member_nam',
      width: 100,
      form: {
        type: 'a-input',
        placeholder: '输入家庭成员姓名'
      }
    },
    {
      title: '性别',
      key: 'member_sex',
      width: 70,
      scopedSlots: { customRender: 'member_sex' },
      form: {
        type: 'a-radio-group',
        placeholder: '选择性别',
        defaultValue: 1,
        dict: [
          {
            label: '男',
            value: '1'
          },
          {
            label: '女',
            value: '2'
          }
        ]
      }
    },
    {
      title: '身份证号',
      dataIndex: 'member_sfz',
      key: 'member_sfz',
      width: 180,
      form: {
        type: 'a-input',
        placeholder: '输入身份证号'
      }
    },
    {
      title: '家庭住址',
      // dataIndex: 'member_addr',
      key: 'member_addr',
      width: 120,
      form: {
        type: 'a-textarea',
        placeholder: '输入家庭住址',
        renderMode: ['add', 'edit'],
      }
    },
    // {
    //   title: '户口本号',
    //   dataIndex: 'family_book_id',
    //   key: 'family_book_id',
    //   form: {
    //     type: 'a-input',
    //     placeholder: '输入户口本号',
    //     isShowQuery: true
    //   }
    // },
    {
      title: '手机号',
      dataIndex: 'member_tel',
      key: 'member_tel',
      width: 110,
      form: {
        type: 'a-input',
        placeholder: '输入手机号'
      }
    },

    {
      title: '银行卡号',
      dataIndex: 'member_bank_num',
      key: 'member_bank_num',
      width: 180,
      form: {
        type: 'a-input',
        placeholder: '输入银行卡号'
      }
    },
    {
      title: '开户银行',
      key: 'member_bank_code',
      width: 180,
      scopedSlots: { customRender: 'member_bank_code' },
      form: {
        type: 'a-auto-complete',
        placeholder: '选择/输入 开户银行',
        showSearch: true,
        dataSource: []
      }
    },
    {
      title: '备注',
      dataIndex: 'readme',
      key: 'readme',
      form: {
        type: 'a-textarea',
        placeholder: '输入备注'
      }
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      fixed: 'right',
      scopedSlots: { customRender: 'action' }
    }
  ]

  public tableApi = {
    list: famerServices.getMemberList,
    add: famerServices.updateMember,
    edit: famerServices.updateMember,
    del: famerServices.deleteMember
    // imageUpload: 'uploadStoreImage',
    // imageRemove: 'delStoreImage'
  }

  @Watch('currentAreaId')
  public handlerCurrentAreaIdChange() {
    this.commonParams.s_a_id = this.currentAreaId
    this.getData()
  }

  @Watch('currentMember.family_id', { immediate: true })
  public handlerCurrentMemberChange() {
    this.updateFamily(this.currentMember)
  }

  public mounted() {
    this.getBankTypes()
    this.events.listen('select-houseHolder', this.updateFamily.bind(this))
    this.$on('hook:beforeDestroy', () => {
      this.events.remove('select-houseHolder')
    })
  }

  public updateFamily(data: IMember) {
    this.currentHouseHolder = data
    this.commonParams.family_id = data.family_id
    this.commonParams.s_a_id = data.s_a_id
    if (!data.family_id) {
      this.tableData = []
      return
    }
    this.getData()
  }

  public async getData() {
    if (!this.commonParams.family_id) return
    const res = await this.getTableList()
  }

  public getBankTypes() {
    commonServices.getBankType().then(res => {
      const dict = []
      this.changeDict(
        'member_bank_code',
        Object.keys(res.data).map(key => res.data[key]),
        'dataSource'
      )
    })
  }

  public handlerTableAdd() {
    this.tableAdd()
    this.$nextTick(() => {
      this.currentRow = Object.assign(
        {},
        { role: undefined }
      )
      this.visibleTitle = '添加农户'
    })
    this.visibleTitle = '添加家庭成员'
  }

  // 编辑前数据预处理
  public async handleTableEdit(row: any) {
    this.tableEdit(Object.assign({}, row))
  }

  public handlerSetHolder(row: IMember) {
    famerServices
      .setMemberToHolder({
        iid: row.iid,
        member_id: row.member_id,
        s_a_id: row.s_a_id
      })
      .then(() => {
        this.events.trigger('update-holder')
        // 刷新当前列表
        this.updateFamily(row)
        this.$message.success('更新户主成功')
      })
  }
}
</script>

<style scoped>
.action-button {
  margin: 0px 10px 0px 10px;
}
.form-item-class {
  margin-bottom: 0px;
}
.img-box {
  width: 100px;
  height: 100px;
  margin: 10px;
}
.item-blue {
  color: #1989fa;
}
</style>
